<?php
// +----------------------------------------------------------------------
// | JavaJY Framework
// +----------------------------------------------------------------------
// | Copyright (c) http://www.javajy.com
// +----------------------------------------------------------------------
// | Author www.javajy.com <QQ:262042083>
// +----------------------------------------------------------------------
namespace Admin\Controller;


class NavController extends BaseController{
    
	function edit(){
		$id = I('id');
		if(!$id){
			$this->error('无效的参数');
			exit;
		}
		$db = M('navigation');
		$re = $db->where(array('id'=>$id))->find();
		
		if($re){
			$this->assign('content',$re);
			$re = $db->order('ord desc,id asc')->select();
			foreach($re as $k=>$v){
				if($v['id'] == $id) unset($re[$k]);
			}
			$this->nav = $re;
			$this->display();
		}else{
			$this->error('无效的参数');
		}
	}
	
	function editok(){
		if(IS_POST){
			if($_POST['name'] == ''){
				$this->error('名称不能为空');
				exit;
			}
		}else{
			$this->error('无效的访问');
			exit;
		}
		
		$db = M('navigation');
		$db->create();
		$rid = $db->save();
		if($rid){
			$this->success('修改成功！',U('index'));
		}else{
			$this->error('修改失败！');
		}	
	}
	function index(){
		$db = M('navigation');
		//$nav = $db->where(array('type'=>TYPE,'pid'=>0))->order('ord desc, id asc')->select();
		$nav = $db->order('ord desc, id asc')->select();
		$this->nav = getNavSon($nav);
		$this->display();
	}
	
	function add(){
		$db = M('navigation');
		//$nav = $db->where(array('type'=>'TYPE','pid'=>0))->order('ord desc, id asc')->select();
		$nav = $db->order('ord desc, id asc')->select();
		$this->nav = getNavSon($nav);
		$this->display();
	}
	
	function addok(){
		if(IS_POST){
			if($_POST['name'] == ''){
				$this->error('名称不能为空');
				exit;
			}
			if($_POST['url'] == ''){
				$this->error('链接不能为空');
				exit;
			}
		}else{
			$this->error('无效的访问');
			exit;
		}
		
		$db = M('navigation');
		//上级导航
		if(I('post.pid')){
			$db->where(array('id'=>I('post.pid')))->save(array('level'=>1));
		}
		$db->create();
		$rid = $db->add();
		if($rid){
			$this->success('添加成功！',U('index'));
		}else{
			$this->error('添加失败！');
		}
		
	}
	
	function delete(){
		$id = I('id');
		if(!$id){
			$this->error('无效的参数');
			exit;
		}
		$db = M('navigation');
		$rid = $db->where('id = '.I('id'))->delete();
		if($rid){
			$this->success('删除成功！',U('index'));
		}else{
			$this->error('删除失败!');
		}
		
	}
}